package com.ruoyi.park.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.park.mapper.WyRepairManagementMapper;
import com.ruoyi.park.domain.WyRepairManagement;
import com.ruoyi.park.service.IWyRepairManagementService;

/**
 * 维修管理Service业务层处理
 *
 * @author yy
 * @date 2021-09-28
 */
@Service
public class WyRepairManagementServiceImpl implements IWyRepairManagementService {
    @Autowired
    private WyRepairManagementMapper wyRepairManagementMapper;

    /**
     * 查询维修管理
     *
     * @param id 维修管理ID
     * @return 维修管理
     */
    @Override
    public WyRepairManagement selectWyRepairManagementById(String id) {
        return wyRepairManagementMapper.selectWyRepairManagementById(id);
    }

    /**
     * 查询维修管理列表
     *
     * @param wyRepairManagement 维修管理
     * @return 维修管理
     */
    @Override
    public List<WyRepairManagement> selectWyRepairManagementList(WyRepairManagement wyRepairManagement) {
        return wyRepairManagementMapper.selectWyRepairManagementList(wyRepairManagement);
    }

    /**
     * 新增维修管理
     *
     * @param wyRepairManagement 维修管理
     * @return 结果
     */
    @Override
    public int insertWyRepairManagement(WyRepairManagement wyRepairManagement) {
        wyRepairManagement.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        wyRepairManagement.setCreateTime(DateUtils.getNowDate());
        wyRepairManagement.setCreateBy(SecurityUtils.getUsername());
        return wyRepairManagementMapper.insertWyRepairManagement(wyRepairManagement);
    }

    /**
     * 修改维修管理
     *
     * @param wyRepairManagement 维修管理
     * @return 结果
     */
    @Override
    public int updateWyRepairManagement(WyRepairManagement wyRepairManagement) {
        Date eTime=DateUtils.getNowDate();
        wyRepairManagement.setUpdateTime(eTime);
        String state= wyRepairManagement.getState();
        if (state.equals("4")){
            Date sTime=wyRepairManagement.getCreateTime();
            String str=DateUtils.compareTime(eTime,sTime);
//            long duration = DateUtil.between(wyRepairManagement.getCreateTime(), wyRepairManagement.getUpdateTime(),
//                    DateUnit.SECOND);
            wyRepairManagement.setDuration(str);
        }
        wyRepairManagement.setUpdateBy(SecurityUtils.getUsername());
        return wyRepairManagementMapper.updateWyRepairManagement(wyRepairManagement);
    }


    /**
     * 批量删除维修管理
     *
     * @param ids 需要删除的维修管理ID
     * @return 结果
     */
    @Override
    public int deleteWyRepairManagementByIds(String[] ids) {
        return wyRepairManagementMapper.deleteWyRepairManagementByIds(ids);
    }

    /**
     * 删除维修管理信息
     *
     * @param id 维修管理ID
     * @return 结果
     */
    @Override
    public int deleteWyRepairManagementById(String id) {
        return wyRepairManagementMapper.deleteWyRepairManagementById(id);
    }
}
